#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include "testutils.h"

/*
 * Testing the Stweward card.
 * Cost: 3
 * Effect: Choice 1 (1) -> +2 cards
 *		   Choice 1 (2) -> +2 coins
 *		   Otherwise    -> trash 2 cards in hand
 */
void test_steward()
{
	int i, j;
	int fail_count = 0;
	int prev_coin_count;
	int prev_handcount;
	int steward_choice;
	int discard_choice1;
	int discard_choice2;
	int prev_discard_choice1_count;
	int prev_discard_choice2_count;
	int prev_steward_count;
	struct gameState state;

	init_test_data(&state, 1);

	srand(time(NULL));

	for (i = 0; i < MAX_ITERATIONS; i++) {
		state.deckCount[0] = 50;
		for (j = 0; j < state.deckCount[0]; j++) {
			state.deck[0][j] = random_card();
		}

		state.handCount[0] = 5;
		for (j = 0; j < 4; j++) {
			state.hand[0][j] = random_card();
		}
		state.hand[0][4] = steward;

		prev_coin_count = state.coins;
		prev_handcount = state.handCount[0];
		prev_steward_count = card_count(state.hand[0], steward, state.handCount[0]);

		steward_choice = rand() % 3;

		if (steward_choice == 1 || steward_choice == 2) {
			cardEffect(steward, steward_choice, 0, 0, &state, 4, 0);
		}
		else {
			/* In this case, we want to select, at random, two of the cards in our hand. */
			discard_choice1 = rand() % 4;
			do {
				discard_choice2 = rand() % 4;
			} while (discard_choice2 != discard_choice1);
			
			prev_discard_choice1_count = card_count(state.hand[0], state.hand[0][discard_choice1], state.handCount[0]);
			prev_discard_choice2_count = card_count(state.hand[0], state.hand[0][discard_choice2], state.handCount[0]);

			cardEffect(steward, 0, discard_choice1, discard_choice2, &state, 4, 0);
		}

		if (steward_choice == 1) {
			if (state.handCount[0] != prev_handcount + 1) {
				printf("FAIL iteration %d: did not draw two new cards with choice: %d.\n", i, steward_choice);
				fail_count++;
			}
		}
		else if (steward_choice == 2) {
			if (state.coins != prev_coin_count + 2) {
				printf("FAIL iteration %d: did not get two more coins with choice: %d.\n", i, steward_choice);
				fail_count++;
			}
		}
		else {
			if (state.handCount[0] != prev_handcount - 3) {
				printf("FAIL iteration %d: did not discard two cards discard choice.", i);
				fail_count++;
			}
			else
			{
				if (card_count(state.hand[0], state.hand[0][discard_choice1], state.handCount[0]) >= prev_discard_choice1_count) {
					printf("FAIL iteration %d: did not discard the discard choice 1.\n", i);
					fail_count++;
				}
				if (card_count(state.hand[0], state.hand[0][discard_choice2], state.handCount[0]) >= prev_discard_choice2_count) {
					printf("FAIL iteration %d: did not discard the discard choice 2.\n", i);
					fail_count++;
				}
			}
		}

		if (card_count(state.hand[0], steward, state.handCount[0]) >= prev_steward_count) {
			printf("FAIL interation %d: did not discard the played steward card.\n", i);
			fail_count++;
		}
	}

	if (fail_count > 0) {
		printf("Test failed!  Number of failures: %d\n", fail_count);
	}
	else {
		printf("All tests passed!\n");
	}
}

int main(void)
{
	test_steward();
	return 0;
}